package com.cy.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.Map;

@RestController
@RequestMapping("/consumer/")
public class RestConsumerController {
    @Autowired
    private RestTemplate loadBalancedRestTemplate;
    @DeleteMapping("{id}")
    public String doDeleteById(@PathVariable String id){
        String url = String.format("http://%s/provider/%s","sca-provider",id);
         loadBalancedRestTemplate.delete(url,id);
         return "delete ok";
    }
    @PostMapping
    public Map<String, Object>  doCreate(@RequestBody Map<String, Object> map){
        String url = String.format("http://%s/provider/","sca-provider");
        return loadBalancedRestTemplate.postForObject(url,map,Map.class);



    }
    @PutMapping
    public Map<String,Object>  doupdate(@RequestBody Map<String, Object> map){
        String url = String.format("http://%s/provider/","sca-provider");
        HttpEntity httpEntity = new HttpEntity(map);
        ResponseEntity<Map> exchange = loadBalancedRestTemplate.exchange(url, HttpMethod.PUT, httpEntity, Map.class);
        return  exchange.getBody();


    }

}
